import DBContainer from "./database.js";
import { Model } from "sequelize";
import { empty } from "#Utils/helper.js";

class CustomModel extends Model {
    static connect = '';
    static attributes = {}
    static options = {}
    constructor(...args) {
        super(...args);
    }
    static async getDB() {
        if (!this.getConnect()) {
            throw new Error('Connect not set');
        }
        return await DBContainer.getDB(this.getConnect());
    }

    static setConnect(connect) {
        this.constructor.connect = connect;
    }

    static getConnect() {
        return this.constructor.connect;
    }

    static setAttributes(attributes) {
        this.constructor.attributes = { ...this.constructor.attributes, ...attributes };
    }

    static getAttributes() {
        return this.constructor.attributes;
    }

    static setOptions(options) {
        this.constructor.options = { ...this.constructor.options, ...options };
    }

    static getOptions() {
        return this.constructor.options;
    }

    static async initModel() {
        this.init(this.getAttributes(), this.getOptions());
        return this;
    }

    static async close(connect = '') {

        if (empty(connect)) {
            connect = this.getConnect();
        }
        if (connect) {
            await DBContainer.close(connect);
        }
    }
}

export default CustomModel;